package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdVo;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: AdminAdServiceImpl
 * @Description: TODO
 * @Author: jenyutao@gmail.com
 * @date: 2023/5/30 13:34
 * @Version: V1.0
 */
public class AdminAdServiceImpl implements AdminAdService {

    @Override
    public BaseRespVo list(AdVo adVo) {

        MarketAdExample example = new MarketAdExample();
        MarketAdExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if(adVo.getName()!=null && !"".equals(adVo.getName())){
            criteria.andNameLike(adVo.getName());
        }

        if(adVo.getContent()!=null && !"".equals(adVo.getContent())){
            criteria.andContentEqualTo(adVo.getContent());
        }

        int page = Integer.parseInt(adVo.getPage());
        int limit = Integer.parseInt(adVo.getLimit());
        PageHelper.startPage(page, limit);

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        List<MarketAd> marketAds = mapper.selectByExample(example);
        CommonData data = CommonData.data(marketAds);
        BaseRespVo ok = BaseRespVo.ok(data);

        sqlSession.close();
        return ok;
    }

    @Override
    public BaseRespVo create(MarketAd marketAd) {

        TransactionUtil.startTransaction();

        Date date = new Date();
        marketAd.setAddTime(date);
        marketAd.setUpdateTime(date);
        marketAd.setId(null);
        marketAd.setDeleted(false);

        if(marketAd.getLink()!=null &&!"".equals(marketAd.getLink())){
            marketAd.setLink(marketAd.getLink());
        }

        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        int insert = mapper.insert(marketAd);

        TransactionUtil.commit();

        return BaseRespVo.ok(marketAd);

    }

    @Override
    public BaseRespVo update(MarketAd marketAd) {

        Date date = new Date();
        marketAd.setUpdateTime(date);
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        int i = mapper.updateByPrimaryKey(marketAd);

        TransactionUtil.commit();
        return BaseRespVo.ok(marketAd);
    }

    @Override
    public BaseRespVo delete(MarketAd marketAd) {

        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();

        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        marketAd.setDeleted(true);
        int i = mapper.updateByPrimaryKey(marketAd);

        TransactionUtil.commit();
        return BaseRespVo.ok();
    }
}
